package com.tiktok.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tiktok.dto.PageResult;
import com.tiktok.dto.Result;
import com.tiktok.dto.request.VideoUploadRequest;
import com.tiktok.entity.Video;
import com.tiktok.service.VideoService;
import com.tiktok.utils.UserContext;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 视频控制器
 */
@RestController
@RequestMapping("/video")
@RequiredArgsConstructor
public class VideoController {

    private final VideoService videoService;

    /**
     * 获取推荐视频列表
     */
    @GetMapping("/recommend")
    public Result<PageResult<Video>> getRecommendVideos(
            @RequestParam(defaultValue = "1") int current,
            @RequestParam(defaultValue = "10") int size) {
        
        // 参数验证
        if (current < 1) {
            return Result.badRequest("页码不能小于1");
        }
        if (size < 1 || size > 100) {
            return Result.badRequest("页大小必须在1-100之间");
        }
        
        Long userId = UserContext.getUserId();
        Page<Video> page = videoService.getRecommendVideos(userId, current, size);
        
        PageResult<Video> pageResult = PageResult.of(
                page.getRecords(),
                page.getTotal(),
                page.getCurrent(),
                page.getSize()
        );
        
        return Result.success(pageResult);
    }

    /**
     * 获取用户视频列表
     */
    @GetMapping("/user/{userId}")
    public Result<PageResult<Video>> getUserVideos(
            @PathVariable Long userId,
            @RequestParam(defaultValue = "1") int current,
            @RequestParam(defaultValue = "10") int size) {
        
        // 参数验证
        if (userId == null || userId <= 0) {
            return Result.badRequest("用户ID无效");
        }
        if (current < 1) {
            return Result.badRequest("页码不能小于1");
        }
        if (size < 1 || size > 100) {
            return Result.badRequest("页大小必须在1-100之间");
        }
        
        Page<Video> page = videoService.getUserVideos(userId, current, size);
        
        PageResult<Video> pageResult = PageResult.of(
                page.getRecords(),
                page.getTotal(),
                page.getCurrent(),
                page.getSize()
        );
        
        return Result.success(pageResult);
    }

    /**
     * 获取我的视频列表
     */
    @GetMapping("/my")
    public Result<PageResult<Video>> getMyVideos(
            @RequestParam(defaultValue = "1") int current,
            @RequestParam(defaultValue = "10") int size) {
        
        // 参数验证
        if (current < 1) {
            return Result.badRequest("页码不能小于1");
        }
        if (size < 1 || size > 100) {
            return Result.badRequest("页大小必须在1-100之间");
        }
        
        Long userId = UserContext.getUserId();
        if (userId == null) {
            return Result.unauthorized();
        }
        
        Page<Video> page = videoService.getUserVideos(userId, current, size);
        
        PageResult<Video> pageResult = PageResult.of(
                page.getRecords(),
                page.getTotal(),
                page.getCurrent(),
                page.getSize()
        );
        
        return Result.success(pageResult);
    }

    /**
     * 发布视频
     */
    @PostMapping("/publish")
    public Result<String> publishVideo(@Validated @RequestBody VideoUploadRequest request) {
        videoService.publishVideo(request);
        return Result.success("发布成功");
    }

    /**
     * 删除视频
     */
    @DeleteMapping("/{videoId}")
    public Result<String> deleteVideo(@PathVariable Long videoId) {
        if (videoId == null || videoId <= 0) {
            return Result.badRequest("视频ID无效");
        }
        videoService.deleteVideo(videoId);
        return Result.success("删除成功");
    }

    /**
     * 点赞/取消点赞视频
     */
    @PostMapping("/{videoId}/like")
    public Result<String> likeVideo(@PathVariable Long videoId) {
        if (videoId == null || videoId <= 0) {
            return Result.badRequest("视频ID无效");
        }
        videoService.likeVideo(videoId);
        return Result.success("操作成功");
    }

    /**
     * 记录视频观看
     */
    @PostMapping("/{videoId}/view")
    public Result<String> recordVideoView(@PathVariable Long videoId) {
        if (videoId == null || videoId <= 0) {
            return Result.badRequest("视频ID无效");
        }
        videoService.recordVideoView(videoId);
        return Result.success("记录成功");
    }

    /**
     * 获取上一个视频
     */
    @GetMapping("/{videoId}/previous")
    public Result<Video> getPreviousVideo(@PathVariable Long videoId) {
        if (videoId == null || videoId <= 0) {
            return Result.badRequest("视频ID无效");
        }
        Long userId = UserContext.getUserId();
        Video video = videoService.getPreviousVideo(userId, videoId);
        return Result.success(video);
    }

    /**
     * 获取下一个视频
     */
    @GetMapping("/{videoId}/next")
    public Result<Video> getNextVideo(@PathVariable Long videoId) {
        if (videoId == null || videoId <= 0) {
            return Result.badRequest("视频ID无效");
        }
        Long userId = UserContext.getUserId();
        Video video = videoService.getNextVideo(userId, videoId);
        return Result.success(video);
    }

    /**
     * 获取视频详情
     */
    @GetMapping("/{videoId}")
    public Result<Video> getVideoDetail(@PathVariable Long videoId) {
        if (videoId == null || videoId <= 0) {
            return Result.badRequest("视频ID无效");
        }
        Video video = videoService.getById(videoId);
        if (video == null || video.getStatus() != 1) {
            return Result.notFound();
        }
        return Result.success(video);
    }
} 